package com.tebiecloud.mpweixin.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.common.constants.ResultEnum;
import com.tebiecloud.common.security.SecurityHelper;
import com.tebiecloud.common.security.SecurityUserDetails;
import com.tebiecloud.mpweixin.client.model.entity.MpweixinUserFlow;
import com.tebiecloud.mpweixin.client.model.entity.MpweixinUserWallet;
import com.tebiecloud.mpweixin.client.model.vo.AccountInfoVO;
import com.tebiecloud.mpweixin.client.model.vo.PersonalInfoVO;
import com.tebiecloud.mpweixin.client.model.vo.WithdrawInfoVO;
import com.tebiecloud.mpweixin.server.service.MpweixinUserFlowService;
import com.tebiecloud.mpweixin.server.service.MpweixinUserWalletService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.mpweixin.server.service.feign.YunWithdrawServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 *  前端控制器
 * @author tebie
 * @date 2019-09-16
 */
@RestController
@RequestMapping("/mpweixinuserwallet")
public class MpweixinUserWalletController {

    @Autowired
    private MpweixinUserWalletService mpweixinUserWalletService;

    @Autowired
    private YunWithdrawServiceClient yunWithdrawServiceClient;

    @Autowired
    private MpweixinUserFlowService mpweixinUserFlowService;

    /**
     * 个人余额信息
     * @return
     */
    @PostMapping("account/info")
    public ResultBody<AccountInfoVO> infoPersonal() {
        return ResultBodyUtils.ok(mpweixinUserWalletService.infoAccount());
    }

    /**
     * 获取未到账余额列表
     * @return
     */
    @PostMapping("notarrivedlist")
    public ResultBody notarrivedlist() {
        SecurityUserDetails securityUserDetails = SecurityHelper.getUser();
        return ResultBodyUtils.ok(mpweixinUserWalletService.selectNotArriedList(securityUserDetails.getUserId()));
    }

    /**
     * 获取可提现信息
     * @return
     */
    @PostMapping("withdraw/Info")
    public ResultBody<WithdrawInfoVO> infoWithdraw() {
        return ResultBodyUtils.ok(mpweixinUserWalletService.infoWithdraw());
    }

    /**
     * 提现
     * @return
     */
    @PostMapping("withdraw")
    public ResultBody withdraw(@RequestParam("amount") int amount) {
        SecurityUserDetails securityUserDetails = SecurityHelper.getUser();
        long userId = securityUserDetails.getUserId();
        if(amount<=100){
            return ResultBodyUtils.failed("起提资金不能小于等于1元");
        }
        //查询用户余额
        QueryWrapper<MpweixinUserWallet> mpweixinUserWalletQueryWrapper = new QueryWrapper<>();
        mpweixinUserWalletQueryWrapper.eq("user_id",userId);
        MpweixinUserWallet mpweixinUserWallet = mpweixinUserWalletService.getOne(mpweixinUserWalletQueryWrapper);
        if(mpweixinUserWallet==null){
            mpweixinUserWallet = mpweixinUserWalletService.createOrFind(userId);
        }
        if(mpweixinUserWallet.getBalance()<amount){
            return ResultBodyUtils.failed("账号余额不足");
        }
        ResultBody<com.tebiecloud.yunst.client.model.vo.WithdrawInfoVO> withdrawResult = yunWithdrawServiceClient.withdrawApply(securityUserDetails.getUserId(),amount);
        if(withdrawResult!=null && withdrawResult.getCode()== ResultEnum.OK.getCode()){
            com.tebiecloud.yunst.client.model.vo.WithdrawInfoVO withdrawInfoVO1 = withdrawResult.getData();
            //新增流水
            mpweixinUserFlowService.saveFlow(securityUserDetails.getUserId(),Long.valueOf(withdrawInfoVO1.getNumber()),withdrawInfoVO1.getAmount(),4);
            //更新金额
            mpweixinUserWalletService.updateDeductAmount(securityUserDetails.getUserId(),withdrawInfoVO1.getAmount(),0);
        }else{
            return withdrawResult;
        }
        return ResultBodyUtils.ok();
    }

}
